/**
 * Created by zoulu on 15/10/14.
 */

var Manage = {
	init: function() {
		var uploader = Qiniu.uploader({
			runtimes: 'html5,flash,html4',    //上传模式,依次退化
			browse_button: 'pickfiles',       //上传选择的点选按钮，**必需**
//			uptoken_url: 'http://t1.7kez.com/image/uptoken',
			uptoken: getToken(),
		 	unique_names: true,
			domain: 'http://7xkuzk.com2.z0.glb.qiniucdn.com/',
			container: 'container',           //上传区域DOM ID，默认是browser_button的父元素，
			max_file_size: '100mb',           //最大文件体积限制
			flash_swf_url: 'js/plupload/Moxie.swf',  //引入flash,相对路径
			max_retries: 3,                   //上传失败最大重试次数
			dragdrop: true,                   //开启可拖曳上传
			drop_element: 'container',        //拖曳上传区域元素的ID，拖曳文件或文件夹后可触发上传
			chunk_size: '4mb',                //分块上传时，每片的体积
			auto_start: true,                 //选择文件后自动上传，若关闭需要自己绑定事件触发上传
			init: {
			    'FilesAdded': function(up, files) {
			        plupload.each(files, function(file) {
			        });
			    },
			    'BeforeUpload': function(up, file) {
			    },
			    'UploadProgress': function(up, file) {
			    },
			    'FileUploaded': function(up, file, info) {
			    		
			            var domain = up.getOption('domain');
			            var res = $.parseJSON(info);
			            var sourceLink = domain + res.key; //获取上传成功后的文件的Url
			            console.log("sourceLink = " + sourceLink);
			            //回调地址
			            callback_image_uploaded(sourceLink);
			    },
			    'Error': function(up, err, errTip) {
			    },
			    'UploadComplete': function() {
			    },
			    'Key': function(up, file) {
			        var key = "";
			            return key
			        }
			    }
			}
		);
		
		
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = "";
		if (url.indexOf("?") != -1) {
			theRequest = url.split("=")[1];
		}
		$("#add_photo").find("input[name='school_id']").attr("value", theRequest)
		Manage.get_data(0, 10, theRequest);
		$(".pagination li a").click(function() {
			var page = $(this).attr("page");
			var size = 10;
			Manage.get_data(page, size, theRequest);
			Common.init_pagination(page);
		});
		
		$("#data_table tbody").find("a[name='delete_pohoto']").click(Manage.delete_pohoto);
	
	},

	get_data: function(page, size, theRequest) {
		if (page == undefined) {
			page = 0;
		}
		if (size == undefined) {
			size = 10;
		}
		ajax("/schools/images/list", {
			"page": page,
			"size": size,
			"school_id": theRequest
		}, callback_init_table);
	},
	
	add_photo:function(url){
		var school_id = $("#add_photo").find("input[name='school_id']").val();
		
			ajax("/schools/images/add", {
				"school_id": school_id,
				"image": url
			},  success);
		
	},
	delete_pohoto: function(theRequest) {
		var img_url = $(this).attr("imgurl");
		var school_id = $("#add_photo").find("input[name='school_id']").val();
		if (confirm("确定要删除该图片?")) {

			ajax("/schools/images/remove", {
				"image_url": img_url,
				"school_id": school_id
			}, success);
		}
	},
	init_table: function(result) {
		if (result != null && result.data != null && result.data.images.length > 0) {
			var tbody = $("#data_table tbody");
			tbody.empty();
			var images = result.data.images;

			var trs = "";
			for (var i = 0; i < images.length; i++) {
				var img = ("<tr><td> <img src=\"" + images[i]+ "\"/> </td>");
				var operate = ("<td> <a height='50' width='50' imgurl='" + images[i]+ "' name='delete_pohoto' href='javascript:void(0)'>删除</a> </td>");

				trs += (img + operate);
				trs += ("</tr>");
			}
			tbody.append(trs);
		}
	}
};

function callback_image_uploaded(sourceLink) {
	Manage.add_photo(sourceLink);
	
	
}
function callback_init_table(result) {
	Manage.init_table(result);
}

function success(result) {
	if (result != null && result.code == 0) {
		window.location.reload();
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function() {
	Manage.init();
})